<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><%
	var cuid=Array.isArray(table.id)?table.id[0]:table.id;
%>
<mapper namespace="priv.mill.dao.mapper.<%=xia2Pasic(table.attr["@_name"])%>Mapper">
	<resultMap type="priv.mill.entry.<%=xia2Pasic(table.attr["@_name"])%>" id="<%=xia2Pasic(table.attr["@_name"])%>">
		<%
			//循环id
			[].concat(table.id).forEach((item)=>{
		%><id column="<%=item.attr['@_name']%>" property="<%=xia2TuoFeng(item.attr['@_name'])%>" /><%
			});
		%>
		<%
			//循环id
			[].concat(table.field).forEach((item)=>{
		%><result column="<%=item.attr['@_name']%>" property="<%=xia2TuoFeng(item.attr['@_name'])%>" />
		<%
			});
		%>

		<%
			//平装association。为解决：只能引用ID字段，否则没有对应的查询方法
			if(table.beRefs)
			[].concat(table.beRefs).forEach((item)=>{
		%><association property="<%=xia2TuoFeng(item.table)%>" javaType="priv.mill.entry.<%=xia2Pasic(item.table)%>"
			column="<%=item.col%>" select="priv.mill.dao.mapper.<%=xia2Pasic(item.table)%>Mapper.get<%=xia2Pasic(item.table)%>ById" /><%
			});
		%>

		<%
			//平装collection。为解决：只能引用ID字段，否则没有对应的查询方法
			if(table.refs)
			[].concat(table.refs).forEach((item)=>{
		%><collection property="<%=xia2TuoFeng(item.table)%>" ofType="priv.mill.entry.<%=xia2Pasic(item.table)%>"
			column="<%=item.col%>" select="priv.mill.dao.mapper.<%=xia2Pasic(item.table)%>Mapper.get<%=xia2Pasic(item.table)%>ById" /><%
			});
		%>

		
		
	</resultMap>
	
	<sql id="fields"><%
	var fieldstr=[].concat(table.field,table.id).map((item)=>{
		return item.attr["@_name"];
	}).join();
	%><%=fieldstr%></sql>
	<!-- <%=xia2Pasic(table.attr["@_name"])%> get<%=xia2Pasic(table.attr["@_name"])%>ById(Integer id); -->
	<select id="get<%=xia2Pasic(table.attr["@_name"])%>ById" resultMap="<%=xia2Pasic(table.attr["@_name"])%>">
		SELECT
		<include refid="fields"></include>
		FROM <%=table.attr["@_name"]%>
		WHERE
		<%=table.attr["@_name"]%>.<%=cuid.attr["@_name"]%>= #{<%=xia2TuoFeng(cuid.attr["@_name"])%>}
	</select>

	<!-- <%=xia2Pasic(table.attr["@_name"])%> get<%=xia2Pasic(table.attr["@_name"])%>ByEntry(<%=xia2Pasic(table.attr["@_name"])%> user); -->
	<select id="get<%=xia2Pasic(table.attr["@_name"])%>ByEntry" resultMap="<%=xia2Pasic(table.attr["@_name"])%>">
		select
		<include refid="fields"></include>
		from <%=table.attr["@_name"]%>
		<where>
			<% //循环ID字段
			[].concat(table.id).forEach((item)=>{
			%><if test="<%=xia2TuoFeng(item.attr["@_name"])%>!=null">
				and <%=table.attr["@_name"]%>.<%=item.attr["@_name"]%>=#{<%=xia2TuoFeng(item.attr["@_name"])%>}
			</if><%
			});
			%>

			<% //循环字段
			[].concat(table.field).forEach((item)=>{
			%><if test="<%=xia2TuoFeng(item.attr["@_name"])%>!=null">
				and <%=table.attr["@_name"]%>.<%=item.attr["@_name"]%>=#{<%=xia2TuoFeng(item.attr["@_name"])%>}
			</if><%
			});
			%>
			
		</where>
	</select>
	<!-- int update<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2Pasic(table.attr["@_name"])%> user); -->
	<update id="update<%=xia2Pasic(table.attr["@_name"])%>" parameterType="priv.mill.entry.<%=xia2Pasic(table.attr["@_name"])%>">
		update <%=table.attr["@_name"]%>
		<set>
		<% //循环字段
			[].concat(table.field).forEach((item)=>{
			%><if test="<%=xia2TuoFeng(item.attr["@_name"])%> != null"><%=item.attr["@_name"]%>=#{<%=xia2TuoFeng(item.attr["@_name"])%>},</if><%
			});
			%>
			
		</set>
		where <%=table.attr["@_name"]%>.<%=cuid.attr["@_name"]%>= #{<%=xia2TuoFeng(cuid.attr["@_name"])%>}
	</update>
	<!-- int delete<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2Pasic(table.attr["@_name"])%> user); -->
	<delete id="delete<%=xia2Pasic(table.attr["@_name"])%>">
		delete from <%=table.attr["@_name"]%> where
		<%=table.attr["@_name"]%>.<%=cuid.attr["@_name"]%>= #{<%=xia2TuoFeng(cuid.attr["@_name"])%>}
	</delete>
	<!-- int insert<%=xia2Pasic(table.attr["@_name"])%>(<%=xia2Pasic(table.attr["@_name"])%> user); -->
	<update id="insert<%=xia2Pasic(table.attr["@_name"])%>" parameterType="priv.mill.entry.<%=xia2Pasic(table.attr["@_name"])%>"
		useGeneratedKeys="true" keyProperty="uid">
		insert into <%=table.attr["@_name"]%>
		(<%[].concat(table.field).forEach((item,index,arr)=>{
			var decim=index==arr.length-1?"":",";
			%><%=item.attr["@_name"]%><%=decim%><%})%>)
		values
		(<%[].concat(table.field).forEach((item,index,arr)=>{
			var decim=index==arr.length-1?"":",";
			%>#{<%=xia2TuoFeng(item.attr["@_name"])%>}<%=decim%><%})%>)
	</update>
	<!-- List<<%=xia2Pasic(table.attr["@_name"])%>> get<%=xia2Pasic(table.attr["@_name"])%>sByPage(@Param("pageNumber")int pageNumber ,@Param("pageSize")int 
		pageSize,<%=xia2Pasic(table.attr["@_name"])%> user); -->
	<select id="get<%=xia2Pasic(table.attr["@_name"])%>sByPage" resultMap="<%=xia2Pasic(table.attr["@_name"])%>">
		select * from `<%=table.attr["@_name"]%>`
		<where>
			<% //循环ID字段
			[].concat(table.id).forEach((item)=>{
			%><if test="<%=xia2TuoFeng(item.attr["@_name"])%>!=null">
				and <%=table.attr["@_name"]%>.<%=item.attr["@_name"]%>=#{<%=xia2TuoFeng(item.attr["@_name"])%>}
			</if><%
			});
			%>

			<% //循环字段
			[].concat(table.field).forEach((item)=>{
			%><if test="<%=xia2TuoFeng(item.attr["@_name"])%>!=null">
				and <%=table.attr["@_name"]%>.<%=item.attr["@_name"]%>=#{<%=xia2TuoFeng(item.attr["@_name"])%>}
			</if><%
			});
			%>
			
		</where>
		limit
		#{pageNumber},#{pageSize}
	</select>
</mapper>